﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Text.RegularExpressions;

namespace AutoEncodeDaemon
{
    public partial class Viewer : Form
    {
        private AutoEncodeDBContext db;
        public Viewer(AutoEncodeDBContext dbcontext)
        {
            db = dbcontext;
            InitializeComponent();
        }

        private void Viewer_Load(object sender, EventArgs e)
        {
            toolStripTables.SelectedIndex = 0;
        }

        private void toolStripSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                bindingNavigator.BindingSource.EndEdit();
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void toolStripTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                BindingSource bs = new BindingSource();
                switch (toolStripTables.Text)
                {
                    case "CatalogIndex":
                        bs.DataSource = from table in db.CatalogIndex
                                        select table;
                        break;
                    case "EncodeJobs":
                        bs.DataSource = from table in db.EncodeJobs
                                        select table;
                        break;
                    case "JobStatusIndex":
                        bs.DataSource = from table in db.JobStatusIndex
                                        select table;
                        break;
                    case "ReleaseJobs":
                        bs.DataSource = from table in db.ReleaseJobs
                                        select table;
                        break;
                    case "Template":
                        bs.DataSource = from table in db.Template
                                        select table;
                        break;
                    case "TemplateFilter":
                        bs.DataSource = from table in db.TemplateFilter
                                        select table;
                        break;
                    case "TorrentJobs":
                        bs.DataSource = from table in db.TorrentJobs
                                        select table;
                        break;
                }
                dataGridView.Columns.Clear();
                dataGridView.DataSource = bs;
                if (dataGridView.Columns.Contains("JobStatusIndex")) dataGridView.Columns.Remove("JobStatusIndex");
                if (dataGridView.Columns.Contains("ReleaseJobs")) dataGridView.Columns.Remove("ReleaseJobs");
                if (dataGridView.Columns.Contains("TorrentJobs")) dataGridView.Columns.Remove("TorrentJobs");
                if (dataGridView.Columns.Contains("CatalogIndex")) dataGridView.Columns.Remove("CatalogIndex");
                if (dataGridView.Columns.Contains("Template")) dataGridView.Columns.Remove("Template");

                if (dataGridView.Columns.Contains("EncodeStatus")) dataGridView.Columns["EncodeStatus"].DataPropertyName = null;
                if (dataGridView.Columns.Contains("ReleaseStatus")) dataGridView.Columns["ReleaseStatus"].DataPropertyName = null;
                if (dataGridView.Columns.Contains("TorrentStatus")) dataGridView.Columns["TorrentStatus"].DataPropertyName = null;
                if (dataGridView.Columns.Contains("ReleaseCatalog")) dataGridView.Columns["ReleaseCatalog"].DataPropertyName = null;
                bindingNavigator.BindingSource = bs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1) return;
            if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ValueType.Equals(typeof(string))) textBox.Text = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string;
            else textBox.Text = "";
        }

        private void UpdateText_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedCells.Count == 0)
            {
                MessageBox.Show("No cell has been selected!");
                return;
            }
            if (dataGridView.SelectedCells[0].ValueType.Equals(typeof(string))) dataGridView.SelectedCells[0].Value = textBox.Text;
            else MessageBox.Show("Selected cell is not string type!");
        }

        private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            if (dataGridView.Rows[e.RowIndex].IsNewRow) return;
            e.ParsingApplied = false;
            try
            {
                if (dataGridView.Columns[e.ColumnIndex].Name == "EncodeStatus" || dataGridView.Columns[e.ColumnIndex].Name == "ReleaseStatus" || dataGridView.Columns[e.ColumnIndex].Name == "TorrentStatus")
                {
                    if (dataGridView.Rows[e.RowIndex].DataBoundItem != null)
                    {
                        int index = Convert.ToInt32(e.Value.ToString());
                        Type t = dataGridView.Rows[e.RowIndex].DataBoundItem.GetType();
                        t.GetProperty("JobStatusIndex").SetValue(dataGridView.Rows[e.RowIndex].DataBoundItem, db.JobStatusIndex.First(x => x.JobStatus == index), null);
                        e.ParsingApplied = true;
                    }
                }
                else if (dataGridView.Columns[e.ColumnIndex].Name == "ReleaseCatalog")
                {
                    if (dataGridView.Rows[e.RowIndex].DataBoundItem != null)
                    {
                        int catalog = Convert.ToInt32(e.Value.ToString());
                        Type t = dataGridView.Rows[e.RowIndex].DataBoundItem.GetType();
                        t.GetProperty("CatalogIndex").SetValue(dataGridView.Rows[e.RowIndex].DataBoundItem,db.CatalogIndex.First(x => x.CatalogID == catalog), null);
                        e.ParsingApplied = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Encountered an error while setting value for " + dataGridView.Columns[e.ColumnIndex].Name + ": " + ex.ToString());
            }
        }

        private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.FormattingApplied = false;
            try
            {
                if (dataGridView.Columns[e.ColumnIndex].Name == "EncodeStatus" || dataGridView.Columns[e.ColumnIndex].Name == "ReleaseStatus" || dataGridView.Columns[e.ColumnIndex].Name == "TorrentStatus")
                {
                    if (dataGridView.Rows[e.RowIndex].DataBoundItem != null)
                    {
                        Type t = dataGridView.Rows[e.RowIndex].DataBoundItem.GetType();
                        object status = t.GetProperty("JobStatusIndex").GetValue(dataGridView.Rows[e.RowIndex].DataBoundItem, null);
                        if (status != null)
                        {
                            t = status.GetType();
                            e.Value = t.GetProperty("JobStatus").GetValue(status, null).ToString();
                            e.FormattingApplied = true;
                        }
                    }
                }
                else if (dataGridView.Columns[e.ColumnIndex].Name == "ReleaseCatalog")
                {
                    if (dataGridView.Rows[e.RowIndex].DataBoundItem != null)
                    {
                        Type t = dataGridView.Rows[e.RowIndex].DataBoundItem.GetType();
                        object status = t.GetProperty("CatalogIndex").GetValue(dataGridView.Rows[e.RowIndex].DataBoundItem, null);
                        if (status != null)
                        {
                            t = status.GetType();
                            e.Value = t.GetProperty("CatalogID").GetValue(status, null).ToString();
                            e.FormattingApplied = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Encountered an error while getting value for " + dataGridView.Columns[e.ColumnIndex].Name + ": " + ex.ToString());
            }
        }
    }
}
